<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 19:19
 */

namespace app\api\service\goods;


use app\api\model\GoodsOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class SendOrderSer extends Token
{
    /**
     * 1.判断订单信息
     * 2.进行更新操作
     */
    // 用户信息
    private $user;

    // 订单信息
    private $order;

    function __construct($order_id)
    {
        $this->user = User::get(self::getCurrentUid());
        $this->order = GoodsOrder::get($order_id);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeOrder();
    }

    // 判断订单信息
    private function judgeOrder()
    {
        if(!$this->order) {
            throw new ParameterException([
                'msg' => [
                    'goods' => '订单信息为空'
                ]
            ]);
        }
        if($this->order['user_id'] != $this->user['id']) {
            throw new ParameterException([
                'msg' => [
                    'goods' => '不是您的订单'
                ]
            ]);
        }
        if($this->order['status'] != 3) {
            throw new ParameterException([
                'msg' => [
                    'goods' => '订单状态错误'
                ]
            ]);
        }
        return $this->updateOrder();
    }

    // 更新订单信息
    private function updateOrder()
    {
        $goodsOrder = new GoodsOrder();
        $goodsOrder->save([
            'status' => 4,
            'send_time' => time()
        ],[
            'id' => $this->order['id']
        ]);
        return writeJson(202, [], '确认收货成功！');
    }
}